#!/bin/sh
# Copyright ©LanBright. All rights reserved.

### 使用说明 ###
# 1.请在'.xcworkspace'文件同级目录新建 '_gym' 文件夹，并将脚本移入'_gym'目录下
# 2.请确保'scheme'参数为项目实际scheme名称
# 3.请确保'workspace_path'参数为项目实际workspace名称
# 4.info.plist文件增加'GymBuild_AppChannel'字段，标识打包渠道

# !!! 项目workspace名称
workspace_name="LanBKit"
# !!! 项目scheme名称
scheme="LanBKit_Example"
# !!! 'Debug' or 'Release'
configuration="Release"
# !!! 打包输出方式，'app-store', 'ad-hoc', 'enterprise', 'development'，即xcodebuild的method参数
export_method='ad-hoc'
product_name=${scheme}

#计时
SECONDS=0
#取当前时间字符串添加到文件结尾
now=$(date +"%Y_%m_%d_%H_%M_%S")
nowDay=$(date +"%Y_%m_%d")

# 获取脚本当前路径
basepath=$(cd `dirname $0`; pwd)
workspace_path="$basepath/../${workspace_name}.xcworkspace"
output_path="/Users/$USER/Desktop/${product_name}${now}"
archive_path="$output_path/${product_name}_${now}.xcarchive"
ipa_path="$output_path/${product_name}_${now}.ipa"
ipa_name="${product_name}_${now}.ipa"

#输出设定的变量值
echo "===start==={  export_method: ${export_method}  }===end==="
echo "===start==={  workspace_path: ${workspace_path}  }===end==="
echo "===start==={  output_path: ${output_path}  }===end==="
echo "===start==={  archive_path: ${archive_path}  }===end==="
echo "===start==={  ipa_path: ${ipa_path}  }===end==="

#gym打包
fastlane gym\
 --workspace ${workspace_path}\
 --scheme ${scheme}\
 --clean true\
 --configuration ${configuration}\
 --include_bitcode false\
 --archive_path ${archive_path}\
 --export_method ${export_method}\
 --output_directory ${output_path}\
 --output_name ${ipa_name}

# 根据info.plist 命名ipa
if [ ! -f "$ipa_path" ]; then
    echo "未找到ipa包 $ipa_path"
#    exit 2
fi

# 解包ipa
temIpaDirName="TempPayload"
temIpaDirPath="${output_path}/${temIpaDirName}"
if [ -f "$ipa_path" ]; then
    echo "unzip $ipa_path begin ..."
    unzip "$ipa_path" -d "$temIpaDirPath"
fi

# 定位到 *.app 目录及 info.plist
appDir="$temIpaDirPath/Payload/`ls "$temIpaDirPath/"Payload`"
lcmInfoPlist="${appDir}/Info.plist"
echo "info.plist文件路径 lcmInfoPlist : $lcmInfoPlist"

# 获取app名称、版本号、build号
appName=`/usr/libexec/PlistBuddy -c "Print :CFBundleName" $lcmInfoPlist`
echo "appName : $appName"
appVersion=`/usr/libexec/PlistBuddy -c "Print :CFBundleShortVersionString" $lcmInfoPlist`
echo "appVersion : $appVersion"
appBuild=`/usr/libexec/PlistBuddy -c "Print :CFBundleVersion" $lcmInfoPlist`
echo "appBuild : $appBuild"
appBundleId=`/usr/libexec/PlistBuddy -c "Print :CFBundleIdentifier" $lcmInfoPlist`
echo "appBundleId : $appBundleId"
#info.plist - GymBuild_AppChannel : 业务渠道号,用于ipa命名,及共享文件夹目录创建
appChannel=`/usr/libexec/PlistBuddy -c "Print :GymBuild_AppChannel" $lcmInfoPlist`
echo "appChannel : $appChannel"

# rename ipa
new_ipa_name="${now}_${appName}_${appVersion}_channel(${appChannel}).ipa"
new_ipa_path="$output_path/${new_ipa_name}"
if [ -d "$temIpaDirPath" ]; then
    echo "rm ${temIpaDirPath}"
    rm -rf "${temIpaDirPath}"
fi
if [ -f "$ipa_path" ]; then
    cp "${ipa_path}" "${new_ipa_path}"
    rm -rf "${ipa_path}"
fi

#拷贝app.dSYM
appdSYMZip="$output_path/${appName}.app.dSYM.zip"
cd "$archive_path/dSYMs"
zip -r "${appdSYMZip}" "${appName}.app.dSYM"

#输出总用时
echo "===Finished. Total time: ${SECONDS}s==="
